package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.itheima.domain.mongo.RecommendVideo;
import com.itheima.domain.mongo.Video;
import com.itheima.service.mongo.VideoService;
import com.itheima.util.ConstantUtil;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdService idService;

    //查询推荐给指定用户的视频
    @Override
    public PageBeanVo findRecommendVideo(Long userId, Integer pageNum, Integer pageSize) {
        //1. 先从推荐表查询到视频id集合
        Query query = new Query(
                Criteria.where("userId").is(userId)
        ).with(Sort.by(Sort.Order.desc("date"))).skip((pageNum - 1) * pageSize).limit(pageSize);
        List<RecommendVideo> recommendVideoList = mongoTemplate.find(query, RecommendVideo.class);

        //2. 遍历id集合,获取id
        List<Video> videosList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(recommendVideoList)) {
            for (RecommendVideo recommendVideo : recommendVideoList) {
                ObjectId videoId = recommendVideo.getVideoId();//视频id

                //3. 根据id查询视频详情
                Video video = mongoTemplate.findById(videoId, Video.class);
                videosList.add(video);
            }
        }

        //4. 统计下推荐记录数
        long count = mongoTemplate.count(query, RecommendVideo.class);

        //5. 组装返回结果
        return new PageBeanVo(pageNum, pageSize, count, videosList);
    }

    //发布视频
    @Override
    public void save(Video video) {
        video.setVid(idService.getNextId(ConstantUtil.VIDEO_ID));//获取一个自增长的id
        mongoTemplate.save(video);//视频保存

        //todo 下面这段代码 仅仅是为了测试  作用就是将自己的视频推荐给自己
        RecommendVideo recommendVideo = new RecommendVideo();
        recommendVideo.setDate(System.currentTimeMillis());
        recommendVideo.setVid(video.getVid());
        recommendVideo.setUserId(video.getUserId());
        recommendVideo.setVideoId(video.getId());
        recommendVideo.setScore(RandomUtil.randomDouble(70, 99));
        mongoTemplate.save(recommendVideo);
    }

    //根据用户id分页查询视频
    @Override
    public PageBeanVo findByUserId(Long uid, Integer pageNum, Integer pageSize) {
        //1. 设置查询条件
        Query query = new Query(
                Criteria.where("userId").is(uid)
        ).with(Sort.by(Sort.Order.desc("created"))).skip((pageNum - 1) * pageSize).limit(pageSize);

        //2. 查询列表
        List<Video> videoList = mongoTemplate.find(query, Video.class);

        //3. 统计数量
        long count = mongoTemplate.count(query, Video.class);

        //4. 组装返回结果
        return new PageBeanVo(pageNum, pageSize, count, videoList);

    }

    //根据id查询视频
    @Override
    public Video findVideoById(String videoId) {
        return mongoTemplate.findById(new ObjectId(videoId),Video.class );
    }

}
